package com.ecreditpal.audio.netty;

import com.ecreditpal.audio.model.AudioSession;
import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

/**
 * @author lifeng
 * @version 1.0 on 2018/7/18.
 */
public class ChannelUtil {
    public static AttributeKey<AudioSession> SESSION_KEY = AttributeKey.valueOf("session");

    /**
     * 添加新的会话
     * @param channel
     * @param session
     * @return
     */
    public static boolean addChannelSession(Channel channel, AudioSession session) {
        Attribute<AudioSession> sessionAttr = channel.attr(SESSION_KEY);
        return sessionAttr.compareAndSet(null, session);
    }


    public static AudioSession getSessionBy(Channel channel) {
        Attribute<AudioSession> sessionAttr = channel.attr(SESSION_KEY);
        return sessionAttr.get() ;
    }
}
